<%@page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@include file="/common/session.jsp"%>
<html>
<head>
<LINK type="text/css" rel="stylesheet" href="${ctx}/css/style.css"/>
<script type="text/javascript" src="${ctx}/js/calendar.js"></script>
<script type="text/javascript" src="${ctx}/js/xtable.js"></script>
<script type="text/javascript" src="${ctx}/js/validator.js"></script>
<script type="text/javascript" src="${ctx}/dwr/engine.js"></script>
<script type="text/javascript" src="${ctx}/dwr/util.js"></script>
<script type="text/javascript" src="${ctx}/dwr/interface/TemplateService.js"></script>
<script type="text/javascript" src="${ctx}/dwr/interface/TemplateColumnService.js"></script>
<script type="text/javascript" src="${ctx}/dwr/interface/TemplateValueService.js"></script>
<script type="text/javascript" src="${ctx}/js/loading.js"></script>
<title>角色管理</title>
</head>
<body onload="init()">
请选择模版：
<select id="template" name="template" onchange="selectTemplateColumn()">
    <option value="">请选择模版</option>
</select>
<br />
<br />
<form id="templateColumnForm" name="templateColumnForm" action="">
<div style="border:1px solid #c9c9c9" align="center">
    <table id="templateColumnTable" name="templateColumnTable">
    </table>
    <img src="${ctx}/images/common/button_select.jpg" title="查询" style="cursor:pointer" onclick="selectTemplateValuePage()"/>
    <img src="${ctx}/images/common/button_insert.jpg" title="添加" style="cursor:pointer" onclick="insertTemplateValue()"/>
    <img src="${ctx}/images/common/button_update.jpg" title="修改" style="cursor:pointer" onclick="updateTemplateValue()"/>
    <img src="${ctx}/images/common/button_delete.jpg" title="删除" style="cursor:pointer" onclick="deleteTemplateValue()"/>
    <img src="${ctx}/images/common/button_reset.jpg" title="清空" style="cursor:pointer" onclick="$('templateColumnForm').reset()"/>
</div>
</form>
<SCRIPT LANGUAGE="JavaScript" type="">
var columnNum;
var templateValueXThead = new Array(
new XThead("选择", "radio")
);

var templateValueXTable = new XTable("templateValue", templateValueXThead);

templateValueXTable.XTbodyArray = function (model) {
    var array = new Array(model.length);
    array[0] = new XTbody("templateValue", model[0], "radio", "onclick='setTemplateValue(this)'");
    for(var i = 1; i < model.length; i++){
        array[i] = new XTbody("templateValue", model[i]);
    }
    return array;
}

function getTemplateValue(){
    var templateValuesStr = "";
    var elements = $("templateColumnForm").elements;
    var element;
    var type;
    var value;
    for(var i = 0; i < elements.length; i++){
        element = elements[i];
        type = element.type;
        if(type == "text" || type == "select-one" || type == "select-multiple"){
            value = dwr.util.getValue(element.id);
            if(value.length > 0){
                templateValuesStr += "T3.COLUMN_ID = '" + element.id + "' and T3.COLUMN_VALUE like '" + dwr.util.getValue(element.id) + "%'|";
            }
        }
    }
    if(templateValuesStr.length > 0){
        templateValuesStr = templateValuesStr.substring(0, templateValuesStr.length - 1);
    }

    var templateValue = new TemplateValue();
    templateValue.conditionStr = templateValuesStr;
    var templateColumn = new TemplateColumn();
    templateColumn.templateId = dwr.util.getValue("template");
    templateValue.templateColumn = templateColumn;
    return templateValue;
}

templateValueXTable.getXList = function (page){
    var templateValue = getTemplateValue();
    templateValue.pageIndex = page;
    templateValue.pageSize = templateValueXTable.pageSize * columnNum;
    TemplateValueService.selectTemplateValue(templateValue, columnNum, function(list){templateValueXTable.addTable(list);});
}

function selectTemplateValuePage(){
    if(dwr.util.getValue("template").length == ""){
        return false;
    }
    TemplateValueService.selectTemplateValueNum(getTemplateValue(), columnNum, function(num){templateValueXTable.setTotalPage(num);});
}
</SCRIPT>
</body>
</html>
<script type="text/javascript">
function init(){
    TemplateService.selectTemplate(null, callbackSelectTemplate);
}

function callbackSelectTemplate(list){
    dwr.util.addOptions("template", list, "templateId", "templateName");
}

function selectTemplateColumn(){
    var templateId = dwr.util.getValue("template");
    if(templateId.length > 0){
        var templateColumn = new TemplateColumn();
        templateColumn.templateId = templateId;
        TemplateColumnService.selectTemplateColumn(templateColumn, callbackSelectTemplateColumn);
    }
}

function callbackSelectTemplateColumn(list){
    columnNum = list.length;

    dwr.util.removeAllRows("templateColumnTable");
    for(var i = 0; i < columnNum; i++){
        addTemplateColumnTable(list[i], i);
    }

    var templateValueXTheadArray = new Array();
    templateValueXTheadArray[0] = new XThead("选择", "radio");
    for(var i = 0; i < columnNum; i++){
        templateValueXTheadArray[i + 1] = new XThead(list[i].columnName);
    }
    templateValueXTable.setThead(templateValueXTheadArray);

    selectTemplateValuePage();
}

function addTemplateColumnTable(templateColumn, index){
    var table = $("templateColumnTable");

    if(index % 4 == 0){
        var tr = table.insertRow();
    }
    else{
        var num = table.rows.length;
        var tr = table.rows[num - 1];
    }
    var td;

    td = tr.insertCell();
    td.innerText = templateColumn.columnName;

    td = tr.insertCell();
    var optionHtml = "<OPTION value=''>请选择</OPTION>";
    if(templateColumn.columnType == "enum" || templateColumn.columnType == "enums"){
        var templateColumnEnumList = templateColumn.templateColumnEnumList;
        var templateColumnEnum;
        for(var i = 0; i < templateColumnEnumList.length; i++){
            templateColumnEnum = templateColumnEnumList[i];
            optionHtml += "<OPTION value='" + templateColumnEnum.enumValue + "'>" + templateColumnEnum.enumText + "</OPTION>";
        }
    }

    var tdHtml =
    templateColumn.columnType == "enum"
    ?
    "<SELECT " +
    "id='" + templateColumn.columnId + "' " +
    "name='" + templateColumn.columnId + "' " +
    "elname='" + templateColumn.columnName + "' " +
    "required='" + (templateColumn.columnNullFlag == "false" ? "1" : "0") + "' " +
    ">" +
    optionHtml +
    "</SELECT>"
    :
    templateColumn.columnType == "enums"
    ?
    "<SELECT  multiple='multiple' " +
    "id='" + templateColumn.columnId + "' " +
    "name='" + templateColumn.columnId + "' " +
    "elname='" + templateColumn.columnName + "' " +
    "required='" + (templateColumn.columnNullFlag == "false" ? "1" : "0") + "' " +
    ">" +
    optionHtml +
    "</SELECT>"
    :
    "<INPUT " +
    "id='" + templateColumn.columnId + "' " +
    "name='" + templateColumn.columnId + "' " +
    "type='text' " +
    "onfocus='" + (templateColumn.columnType == "date" ? "calendar()" : "") + "' " +
    "elname='" + templateColumn.columnName + "' " +
    "required='" + (templateColumn.columnNullFlag == "false" ? "1" : "0") + "' " +
    "pattern='" + (templateColumn.columnType == "string" || templateColumn.columnType == "date" || templateColumn.columnType == "enum" || templateColumn.columnType == "enums" ? "string" : templateColumn.columnType) + "' " +
    "/ >";
    tdHtml += (templateColumn.columnNullFlag == "false" ? "&nbsp;<font color='red'>*</font>" : "");

    td.innerHTML = tdHtml;
}

function setTemplateValue(obj){
    var elements = $("templateColumnForm").elements;
    var array = new Array();
    for(var i = 0; i < elements.length; i++){
        var element = elements[i];
        var type = element.type;
        if(type == "text" || type == "select-one" || type == "select-multiple"){
            array[array.length] = element;
        }
    }
    var tr = obj.parentElement.parentElement;
    for(var i = 1; i < tr.cells.length; i++){
        if(array[i - 1].type != "select-multiple"){
            dwr.util.setValue(array[i - 1].id, tr.cells[i].innerText);
        }
        else if(array[i - 1].type === "select-multiple"){
            dwr.util.setValue(array[i - 1].id, tr.cells[i].innerText.split(","));
        }
    }
}

function insertTemplateValue(){
    if(validate($("templateColumnForm"))){
        var templateValuesStr = "";
        var elements = $("templateColumnForm").elements;
        var element;
        var type;
        for(var i = 0; i < elements.length; i++){
            element = elements[i];
            type = element.type;
            if(type == "text" || type == "select-one" || type == "select-multiple"){
                templateValuesStr += element.id + "=" + dwr.util.getValue(element.id) + "==";
            }
        }
        if(templateValuesStr.length > 0){
            templateValuesStr = templateValuesStr.substring(0, templateValuesStr.length - 2);
        }
        else{
            return false;
        }
        TemplateValueService.insertTemplateValue("${accountId}", templateValuesStr, selectTemplateValuePage);
    }
}

function updateTemplateValue(){
    if(validate($("templateColumnForm"))){
        var valueId = templateValueXTable.getRadioValue();
        if(valueId == null){
            alert("请选择要修改的记录");
            return false;
        }
        var templateValuesStr = "";
        var elements = $("templateColumnForm").elements;
        var element;
        var type;
        for(var i = 0; i < elements.length; i++){
            element = elements[i];
            type = element.type;
            if(type == "text" || type == "select-one" || type == "select-multiple"){
                templateValuesStr += element.id + "=" + dwr.util.getValue(element.id) + "==";
            }
        }
        if(templateValuesStr.length > 0){
            templateValuesStr = templateValuesStr.substring(0, templateValuesStr.length - 2);
        }
        else{
            return false;
        }
        TemplateValueService.updateTemplateValue(valueId, templateValuesStr, selectTemplateValuePage);
    }
}

function deleteTemplateValue(){
    if(confirm("确定删除选择数据")){
        var valueId = templateValueXTable.getRadioValue();
        if(valueId == null){
            alert("请选择要删除的记录");
            return false;
        }
        var columnIds = "";
        var elements = $("templateColumnForm").elements;
        var element;
        var type;
        for(var i = 0; i < elements.length; i++){
            element = elements[i];
            type = element.type;
            if(type == "text" || type == "select-one" || type == "select-multiple"){
                columnIds += element.id + ",";
            }
        }
        if(columnIds.length > 0){
            columnIds = columnIds.substring(0, columnIds.length - 1);
        }
        else{
            return false;
        }
        TemplateValueService.deleteTemplateValue(valueId, columnIds, selectTemplateValuePage);
    }
}
</script>
